<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>indexedDB</title>
</head>
<body>
	<input type="button" value="创建索引" onclick="creatIndex();" />
	
	<script>
		var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
		window.indexedDB = window.indexedDB || window.webketIndexedDB || window.mozIndexedDB || window.msIndexedDB;
		window.IDBTansaction = window.IDBTansaction || window.webkitIDBTansaction || window.msIDBTansaction;
		window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
		window.IDBCursor = window.IDBCursor || window.webkitIDBCursor || window.msIDBCursor;
		function creatIndex() {
			var dbName = 'indexedDBTest';
			var dbVersion = '201701';
			var idb;
			var dbConnect = indexedDB.open(dbName,dbVersion);//一个IDBOpenDBRequest对象
			dbConnect.onsuccess = function(e) {  //连接成功
				idb = e.target.result;
				console.log("数据库连接成功");
			}
			dbConnect.onerror = function(e) {  //连接失败
				console.log("连接失败");
			}
			dbConnect.onupgradeneeded = function(e) { //当连接的数据库版本号大于当前版本号时触发该事件
				idb = e.target.result;
				var tx = e.target.transaction;
				var name = 'newUsers';
				var optionalParameters = {
					keyPath: 'userId',
					autoIncrement: false
				};
				var store = idb.createObjectStore(name,optionalParameters);
				console.log('对象仓库创建成功');
				var keyPath = 'userName';
				var optionalParameters = {
					unique:false,
					multiEntry: false
				};
				var idx = store.createIndex(name,keyPath,optionalParameters);
				console.log("创建索引成功！");
				
			}
		}
	</script>
	
</body>
</html>